﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using KetBanBonPhuong;
using Telerik.Web.UI;

public partial class Admin_UserControls_StatusManagement : System.Web.UI.UserControl
{
    public static int StatusID = -1;

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            Panel1.Visible = true;
            Panel2.Visible = false;
        }

    }
    protected void RadGrid1_iTemcommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "CommentManager")
        {
            GridDataItem item = (GridDataItem)e.Item;
            StatusID = int.Parse(item["status_id"].Text.ToString());
            Status status = Status.GetInfo(StatusID);
            Label_User.Text = User.GetInfo(status.UserID).UserName;
            Label_Content.Text = status.Content;
            Label_DateCreate.Text = String.Format("{0,2:00}:{1,2:00}:{2,2:00} - {3,2:00}/{4,2:00}/{5,4}",
                status.CreateDate.Hour, status.CreateDate.Minute, status.CreateDate.Second,
                status.CreateDate.Day, status.CreateDate.Month, status.CreateDate.Year);
            Panel1.Visible = false;
            Panel2.Visible = true;
            return;
        }
        
        if (e.CommandName == "Xoa")
        {
            GridDataItem item = (GridDataItem)e.Item;
            int StatusID = int.Parse(item["status_id"].Text.ToString());
            Status.Delete(StatusID);
            return;
        }

    }
    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        DataTable dt = Status.GetAll();
        dt.Columns.Add("username");
        String Content;
        foreach (DataRow dtRow in dt.Rows)
        {
            User user = User.GetInfo((int)dtRow["user_id"]);
            dtRow["username"] = user.UserName;
            Content = dtRow["text"].ToString();
            if (Content.Length > 100)
            {
                Content = Content.Substring(0, 100);
                Content = Content.Substring(0, Content.LastIndexOf(' '));
                Content += "...";
                dtRow["text"] = Content;
            }
        }

        DataView dv = dt.DefaultView;
        dv.Sort = "created_date DESC";
        RadGrid1.DataSource = dv.ToTable();
        RadGrid1.DataBind();
    }
    protected void RadGrid2_PreRender(object sender, EventArgs e)
    {
        DataTable dt = CommentStatus.GetAll(StatusID);
        dt.Columns.Add("username");
        foreach (DataRow dtRow in dt.Rows)
        {
            Friend friend = Friend.GetInfo((int)dtRow["friend_id"]);
            User user = User.GetInfo(friend.Friend_user_id);
            dtRow["username"] = user.UserName;
        }
        DataView dv = dt.DefaultView;
        dv.Sort = "created_date DESC";
        RadGrid2.DataSource = dv.ToTable();
        RadGrid2.DataBind();
    }

    protected void RadGrid2_iTemcommand(object source, GridCommandEventArgs e)
    {
        if (e.CommandName == "Xoa")
        {
            GridDataItem item = (GridDataItem)e.Item;
            int CommentStatusID = int.Parse(item["status_id"].Text.ToString());
            CommentStatus.Delete(CommentStatusID);
            return;
        }
    }
    protected void Button_OK_Click(object sender, EventArgs e)
    {
        Status.Delete(StatusID);
        Panel1.Visible = true;
        Panel2.Visible = false;
    }
}
